package stackexchange.analyzer;

import static stackexchange.Constants.PROC_OF_USERS;

import java.util.Vector;

import stackexchange.User;
import stackexchange.UserGroup;
import stackexchange.posts.Answer;
import stackexchange.posts.Question;

public class ZScoreAnalyzer implements ExpertAnalyzer {

	Vector<Double> zScores = new Vector<Double>();

	@Override
	public UserGroup analyze(UserGroup userGroup, Vector<Question> questions, Vector<Answer> answers) {
		
		//calculate z-score for all users
		Vector<User> allUsers = userGroup.getUsers();
		for (int i = 1; i < allUsers.size(); i++) {
			int nQuestions = allUsers.get(i).getUsersQuestions().size();
			int nAnswers = allUsers.get(i).getUsersAnswers().size();
			
			if((nQuestions + nAnswers) == 0){
				nAnswers = 1;
			}
			double zScore = (nAnswers - nQuestions)
					/ Math.sqrt(nQuestions + nAnswers);
			allUsers.get(i).setExpertiseScore(zScore);
		}
		//sort
		UserGroup allUsersGroup = 
//				return 
						new UserGroup(allUsers).sortOnExpertiseScore();
		Vector<User> sortedUsers = allUsersGroup.getUsers();
		// take %? procent of users
		Vector<User> expertUsers = new Vector<User>();
		for (int i = 0; i < allUsers.size() * PROC_OF_USERS; i++) {
			expertUsers.add(sortedUsers.get(i));
		}
		return new UserGroup(expertUsers);
	}

}
